home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / hsrc_117.zip / GETABNCH.C < prev    next >
Text File  |  1990-10-29  |  6KB  |  299 lines

  1. /* Does 'threading', finds personal msgs, etc. very fast */
  2.  
  3. #include "msgg.h"
  4. #include "twindow.h"
  5. #include "keys.h"
  6. #include "headedit.h"
  7.  
  8. extern WINDOW *ewnd;
  9.  
  10.  
  11.  
  12. word pascal get_abunch (word messno,char type,int direction,char *str) {
  13.  
  14.  register word x=0;
  15.  register int y;
  16.  int handle;
  17.  int handle2;
  18.  char finished=0;
  19.  word temp;
  20.  static struct _xmsg msgs[5];
  21.  char once;
  22.  char keyhit;
  23.  
  24.  if(type==1) {
  25.     rstrip(str);
  26.     if(!strnicmp(str,"RE: ",4)) memmove(str,&str[4],strlen(&str[4])+1);
  27.     if(!*str || !str) return 0;
  28.  }
  29.  if(direction>=0) {
  30.     messno++;
  31.     direction=1;
  32.  }
  33.  else direction=(-1);
  34.  sprintf(filename,"%sXDATA.%03x",path,areano);
  35.  while ((handle=_open(filename,O_RDONLY | O_BINARY | O_DENYNONE))==-1) {
  36.      if (errno==EACCES) {
  37.         x++;
  38.         if(x>2) {
  39.             error_message(" Can't get access ");
  40.             return 0;
  41.         }
  42.         any_message(" Awaiting access ");
  43.         sleep(1);
  44.      }
  45.      else  {
  46.         error_message(" Can't open datafile ");
  47.         return 0;
  48.      }
  49.  }
  50.  if(direction<0) {
  51.     if(messno>6){
  52.         x=5;
  53.         messno-=5;
  54.     }
  55.     else {
  56.         x=messno-1;
  57.         messno=1;
  58.     }
  59.  }
  60. Loop:
  61.  if(kbhit()) {
  62.     keyhit=get_char();
  63.     if(keyhit==' ' || keyhit==ESC) {
  64.         _close(handle);
  65.         return 0;
  66.     }
  67.  }
  68.  if(direction>0) {
  69.     x=(word)(filelength(handle)/(long)sizeof(struct _xmsg));
  70.     if(messno>x || finished) {
  71.         _close(handle);
  72.         return 0;
  73.      }
  74.      if((x-messno)<6) x-=(messno-1);
  75.      else x=5;
  76.  }
  77.  if(ewnd) {
  78.     wcursor(ewnd,17,0);
  79.     wprintf(ewnd,"%-5u",messno);
  80.  }
  81.  
  82. Skip:
  83.  if ((lseek(handle,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET)==(-1)) || ((temp=(_read(handle,&msgs,(int)(x * sizeof(struct _xmsg)))))<1)) {
  84.     if (eof(handle)==(-1)) perror ("\nSEEK OR READ ERROR");
  85.     _close(handle);
  86.     return 0;
  87.  }
  88.  if((temp/sizeof(struct _xmsg))<x)x=temp/sizeof(struct _xmsg);
  89.  if(direction<0)y=x-1;
  90.  else y=0;
  91.  if(type==0) {  /* Personal mail check */
  92. #ifdef DEBUG
  93.     printf("\nx=%u  y=%u  dir=%d",x,y,direction);
  94. #endif
  95.     while(1) {
  96.         if((msgs[y].m_attr & MSGDELETED) || (msgs[y].attr & MSGREAD)) goto NoGot;
  97. #ifdef DEBUG
  98.     printf("\nx=%u  y=%u  dir=%d",x,y,direction);
  99. #endif
  100.             rstrip(msgs[y].to);
  101.             if (userno==1 && !stricmp(msgs[y].to,"SYSOP")) goto GotIt;
  102.             if (noalias) {
  103.                 for (handle2=0;handle2<(int)noalias;handle2++) {
  104.                     if (!stricmp(rstrip(alias[handle2]),msgs[y].to)) goto GotIt;
  105.                 }
  106.             goto NoGot;
  107.             }
  108. GotIt:
  109.             _close(handle);
  110.             return (messno+y);
  111. NoGot:
  112.         y+=(direction);
  113.         if(direction<0) {
  114.             if(y<0) break;
  115.         }
  116.         else if(y>=x) break;
  117.     }
  118.  }
  119.  else if (type==1) { /* Subject check */
  120.     temp=(word)strlen(str);
  121.     while(1) {
  122.         if (msgs[y].m_attr & MSGDELETED) goto NoGot2;
  123.         rstrip(msgs[y].subj);
  124.         if(!strnicmp(msgs[y].subj,"RE: ",4)) memmove(msgs[y].subj,&msgs[y].subj[4],strlen(&msgs[y].subj[4])+1);
  125.         handle2=(int)temp;
  126.         if(temp>24) {
  127.             if(strlen(msgs[y].subj)<(int)temp) handle2=strlen(msgs[y].subj);
  128.             if(handle2<24) handle2=temp;
  129.         }
  130.         if(!strnicmp(msgs[y].subj,str,handle2)) {
  131.  
  132.             _close(handle);
  133.             return (messno+y);
  134.         }
  135. NoGot2:
  136.         y+=(direction);
  137.         if(direction<0) {
  138.             if(y<0) break;
  139.         }
  140.         else if(y>=x) break;
  141.     }
  142.  }
  143.  else if (type==2) { /* Body check */
  144.      sprintf(filename,"%sXTEXT.%03x",path,areano);
  145.      temp=0;
  146.      while ((handle2=_open(filename,O_RDONLY | O_BINARY | O_DENYNONE))==-1) {
  147.          if (errno==EACCES) {
  148.             temp++;
  149.             if (temp>2) {
  150.                 error_message(" Can't get text access ");
  151.                 _close(handle);
  152.                 return 0;
  153.             }
  154.             any_message(" Awaiting access ");
  155.             sleep(1);
  156.          }
  157.          else {
  158.             error_message(" Can't open textfile ");
  159.             _close(handle);
  160.             return 0;
  161.          }
  162.      }
  163.     while(1) {
  164.      if(!msgs[y].length) goto NoGot3;
  165.      if ((msgs[y].m_attr & MSGDELETED)) goto NoGot3;
  166.      once=0;
  167. TryThatAgain:
  168.      if (lseek(handle2,msgs[y].start,SEEK_SET)) {
  169.          if (eof(handle2)) {
  170.             if (once<2) {
  171.                 once++;
  172.                 goto TryThatAgain;
  173.             }
  174.             perror (" TEXT SEEK ERROR");
  175.             goto NoGot3;
  176.          }
  177.      }
  178.      *buffer=0;
  179.      temp=510;
  180.      if(temp>(msgs[y].length-1)) temp=msgs[y].length-1;
  181.      temp=_read(handle2,buffer,temp);
  182.      buffer[temp+1]=0;
  183.      if(stristr(buffer,str)) {
  184.         _close(handle);
  185.         _close(handle2);
  186.         return (messno+y);
  187.      }
  188. NoGot3:
  189.      y+=(direction);
  190.      if(direction<0) {
  191.         if(y<0) break;
  192.      }
  193.      else if(y>=x) break;
  194. }
  195.     _close(handle2);
  196.  }
  197.  if(direction>0) {
  198.     if((long)(messno+x)>65535L) finished++;
  199.     else messno+=x;
  200.  }
  201.  else {
  202.     if((long)(messno-x)<=0L) {
  203.         _close(handle);
  204.         return 0;
  205.     }
  206.     else messno-=x;
  207.     if (messno>5) x=5;
  208.  }
  209.  goto Loop;
  210. }
  211.  
  212.  
  213.  
  214. word pascal thread (char which,char *body) {
  215.  
  216.     word tempmess;
  217.     int  direction;
  218.  
  219.     if (nomess<=1) return 0;
  220.     tempmess=messno;
  221.     if (messno>=nomess) which='B';
  222.     if (messno<=1) which='F';
  223.     if (which!='B' && which!='F') which='F';
  224.     if(which=='B')  direction=(-1);
  225.     else direction=1;
  226.     if(!*body || !body) {
  227.         any_message(" Match Scanning:       ");
  228.         messno=get_abunch(messno,1,direction,msg.subj);
  229.     }
  230.     else {
  231.         any_message(" Link Scanning:        ");
  232.         messno=get_abunch(messno,2,direction,body);
  233.     }
  234.     if(!messno) {
  235.         messno=tempmess;
  236.         any_message(" No Thread ");
  237.         pause();
  238.         return 0;
  239.     }
  240.     else clear_message();
  241.     return messno;
  242. }
  243.  
  244.  
  245. int pascal isbrktquote (char *line) {
  246.  
  247.     register char *p;
  248.     register int x=0;
  249.  
  250.     p=line;
  251.     while(*p && x<6) {    /* Is there a > in the first 6 chars? */
  252.         if(*p=='<') return 0;    /* Maybe <grin> */
  253.         if(*p=='>') return 1;    /* Yep */
  254.         p++;
  255.         x++;
  256.     }
  257.     return 0;    /* Nope */
  258. }
  259.  
  260.  
  261. char * pascal make_ansi (char fore, char back) {
  262.  
  263.     static char scratch[24];
  264.     static char *fa[16]={"2;30","2;34","2;32","2;36","2,31","2,35","2,33",
  265.                          "2;37","1;30","1;34","1;32","1;36","1;31","1;35",
  266.                          "1;33","1;37"};
  267.     static char *ba[7]={"40","44","42","45","41","46","43"};
  268.     char *p,*pp;
  269.  
  270.     if((fore<1 || fore > 16) || (back<1 || back>7)) {  /* Error checking */
  271.         fore=WHITE;
  272.         back=BLACK;
  273.     }
  274.     else {
  275.         fore--;
  276.         back--;
  277.     }
  278.     strcpy(scratch,"\x1b[0;");
  279.     p=&scratch[4];
  280.     pp=fa[fore];
  281.     while(*pp) {
  282.         *p = *pp;
  283.         p++;
  284.         pp++;
  285.     }
  286.     *p=';';
  287.     p++;
  288.     pp=ba[back];
  289.     while(*pp) {
  290.         *p = *pp;
  291.         p++;
  292.         pp++;
  293.     }
  294.     *p='m';
  295.     p++;
  296.     *p=0;
  297.     return scratch;
  298. }
  299.